package com.polaris.live.im.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.polaris.live.im.db.entity.MessageEntity

/**
 * 消息实体类
 *
 * @author Created by 半仙 on 2023/8/10/010 18:33
 */
@Dao
interface MessageEntityDao {

    /**
     * 清除表所有数据
     */
    @Query("delete from message")
    suspend fun truncateTable()

    //插入消息
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun saveMessage(messageEntity: MessageEntity): Long

    //查询是否有这条消息
    @Query("select * from message where user_id = :userId and msg_id = :msgId limit 1")
    suspend fun findMessageByUserIdAndMsgId(userId: Long, msgId: String): MessageEntity?

    //查询用户最后一条系统消息
    @Query(
        """
        select * 
        from message where user_id = :userId and other_id = :otherId and command = 'systemMessage' 
        order by timestamp desc 
        limit 1
    """
    )
    suspend fun findLastSystemMessageByUniqueId(userId: Long, otherId: Long): MessageEntity?

    //查询用户最后一条陌生人消息
    @Query(
        """
        select m.*
        from message as m
          left join session as s on s.user_id = :userId and s.other_id = m.sender and s.other_id != 0
        where m.user_id = :userId 
          and recipient = :userId 
          and m.command = 'singleMessage' 
          and s.user_id is not null
          and (s.stranger_status or s.stranger_status is null)
        order by m.timestamp desc 
        limit 1
    """
    )
    suspend fun findLastStrangerMessageByUniqueId(userId: Long): MessageEntity?

    //修改某条消息的发送状态
    @Query("update message set state = :state, timestamp = :timestamp where user_id = :userId and msg_id = :msgId")
    suspend fun updateMessageStateByUserIdAndMsgId(
        userId: Long,
        msgId: String,
        timestamp: Long,
        state: Int,
    ): Int

    //修改某条消息的发送状态
    @Query(
        """
        update message 
        set state = :state, timestamp = :timestamp, error_code = :errorCode, error_message = :errorMessage 
        where user_id = :userId and msg_id = :msgId
    """
    )
    suspend fun updateMessageStateErrorByUserIdAndMsgId(
        userId: Long,
        msgId: String,
        timestamp: Long,
        state: Int,
        errorCode: Int,
        errorMessage: String,
    ): Int

    //查找第一页
    @Query("select * from message where user_id = :userId and other_id = :otherId order by timestamp desc limit :pageSize")
    suspend fun getMessageFirstPage(userId: Long, otherId: Long, pageSize: Int): List<MessageEntity>

    //查找下一页
    @Query("select * from message where user_id = :userId and other_id = :otherId and timestamp< :timestamp order by timestamp desc limit :pageSize")
    suspend fun getMessagePage(
        userId: Long,
        otherId: Long,
        timestamp: Long,
        pageSize: Int,
    ): List<MessageEntity>

    //查找某条消息
    @Query("select * from message where user_id = :userId and msg_id = :msgId")
    suspend fun selectMessageByUserIdAndMsgId(userId: Long, msgId: String): MessageEntity?

    //清理与某个用户的消息
    @Query("delete from message where user_id = :userId and other_id = :otherId")
    suspend fun deleteByUniqueId(userId: Long, otherId: Long): Int

    //清理与某些用户的消息
    @Query("delete from message where user_id = :userId and other_id in(:otherIds)")
    suspend fun deleteByUniqueIdIn(userId: Long, otherIds: List<Long>): Int

    //删除与某个用户的某个消息
    @Query("delete from message where user_id = :userId and other_id = :otherId and msg_id = :msgId")
    suspend fun deleteByUserIdAndOtherIdAndMsgId(userId: Long, otherId: Long, msgId: String): Int

    //修改系统打招呼消息的payload   当打招呼消息变成已读时payload里的flagGreetPushMessage = EXT_FLAG_PUSH_MESSAGE
    @Query("update message SET payload = :payload WHERE msg_id = :msgId")
    fun updateFlagGreetPushMessage(msgId: String, payload: String)
}